package com.learn.project2.service.admin.user;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketUser;
import com.learn.project2.bean.MarketUserExample;
import com.learn.project2.mapper.MarketUserMapper;

import com.learn.project2.model.bo.admin.user.UserListBO;
import com.learn.project2.model.vo.admin.user.UserListVO;
import com.learn.project2.model.vo.admin.user.UserVO;
import com.learn.project2.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author wxy
 * @version 1.0
 * @description:
 * @date 2022-01-08 09:41
 */
@Service
//@CacheConfig(cacheNames = "marketUser")
public class UserServiceImpl implements UserService {

    @Autowired
    MarketUserMapper userMapper;

    @Override
    //@Cacheable
    public UserListVO selectUser(String username, Integer id,String mobile, UserListBO userListBO) {
        Integer page = userListBO.getPage();//页码
        Integer limit = userListBO.getLimit();//当前页数据量
        String sort = userListBO.getSort();//排序列
        String order = userListBO.getOrder();//desc、asc
        //使用分页插件
        PageHelper.startPage(page, limit);
        MarketUserExample userExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = userExample.createCriteria();
        //给username做模糊查询，mobile做精确查询
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != null){
            criteria.andIdEqualTo(id);
        }

        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        criteria.andDeletedEqualTo(false);
        userExample.setOrderByClause(sort + " " + order);
        List<MarketUser> list = userMapper.selectByExample(userExample);

        PageInfo<MarketUser> pageInfo = new PageInfo<>(list);

        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();


        //通过pageHelper来获得分页信息
        /*  PageInfo<UserVO> pageInfo = new PageInfo<>(list);

         */

        return UserListVO.data(total, pages, limit, page, list);
    }

    @Override
    //@Cacheable
    public MarketUser selectUserById(Integer id) {
        MarketUser marketUser =null;
        if (id != null) {
            marketUser = userMapper.selectByPrimaryKey(id);
        }
        return marketUser;
    }

    @Override
    public Integer updateUser(MarketUser marketUser) {
        /*MarketUserExample userExample = new MarketUserExample();
        userExample.*/
        Integer rows = null;
        //marketUser.setId();
        try {
            // rows = userMapper.updateByPrimaryKey(marketUser);
            rows = userMapper.updateByPrimaryKeySelective(marketUser);
        }catch (Exception e){
            return 500;
        }

        return rows;
    }
}